<!DOCTYPE html>
<html lang="zh-cmn-Hans">
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"/>
    <title>页面渲染完整示例 - Vditor</title>
    <meta name="robots" content="index,follow,archive">
    <meta name="description"
          content="Vditor - 易于使用的 Markdown 编辑器，为适配不同的应用场景而生。它使用 TypeScript 实现，支持原生 JavaScript、Vue、React、Angular，提供桌面版。"/>
    <meta name="copyright" content="B3log"/>
    <link rel="canonical" href="https://b3log.org/vditor/demo/preview.html">
    <link rel="apple-touch-icon" href="https://cdn.jsdelivr.net/gh/vanessa219/b3log-index@d6b3ad3964429fe682f3de3ff4a48b59ea3145d9/src/images/brand/vditor-128.png">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta http-equiv="Window-target" content="_top"/>
    <link rel="icon" type="image/png" href="https://cdn.jsdelivr.net/gh/vanessa219/b3log-index@d6b3ad3964429fe682f3de3ff4a48b59ea3145d9/src/images/brand/vditor-128.png"/>
    <link rel="shortcut icon" type="image/x-icon" href="https://cdn.jsdelivr.net/gh/vanessa219/b3log-index@d6b3ad3964429fe682f3de3ff4a48b59ea3145d9/src/images/brand/vditor-512.png"/>
    <link type="text/css" rel="stylesheet" href="https://cdn.jsdelivr.net/gh/vanessa219/b3log-index@7582df6ba7d52434a3e229cdbd56a06ae62b45c6/src/css/base.css" charset="utf-8"/>

    <meta property="og:locale" content="zh_CN"/>
    <meta property="og:title" content="页面渲染完整示例 - Vditor"/>
    <meta property="og:description"
          content="Vditor - 易于使用的 Markdown 编辑器，为适配不同的应用场景而生。它使用 TypeScript 实现，支持原生 JavaScript、Vue、React、Angular，提供桌面版。"/>
    <meta property="og:image" content="https://cdn.jsdelivr.net/gh/vanessa219/b3log-index@d6b3ad3964429fe682f3de3ff4a48b59ea3145d9/src/images/brand/vditor-128.png"/>
    <meta property="og:url" content="https://b3log.org/vditor"/>
    <meta property="og:site_name" content="B3log"/>
    <meta name="twitter:card" content="summary_large_image"/>
    <meta name="twitter:description"
          content="Vditor - 易于使用的 Markdown 编辑器，为适配不同的应用场景而生。它使用 TypeScript 实现，支持原生 JavaScript、Vue、React、Angular，提供桌面版。"/>
    <meta name="twitter:title" content="页面渲染完整示例 - Vditor"/>
    <meta name="twitter:image" content="https://cdn.jsdelivr.net/gh/vanessa219/b3log-index@d6b3ad3964429fe682f3de3ff4a48b59ea3145d9/src/images/brand/vditor-128.png"/>
    <meta name="twitter:url" content="https://b3log.org/vditor"/>
    <meta name="twitter:site" content="@b3logos"/>
    <meta name="twitter:creator" content="@b3logos"/>
    <script src="https://cdn.jsdelivr.net/gh/vanessa219/b3log-index@d6b3ad3964429fe682f3de3ff4a48b59ea3145d9/src/vditor/vditor.js" defer></script>
    <style>
        .header {
            background-color: #fff;
            box-shadow: rgba(0, 0, 0, 0.05) 0 1px 7px;
            border-bottom: 1px solid #e1e4e8;
        }

        .wrapper {
            margin: 0 auto;
            max-width: 768px;
        }

        #outline {
            display: none;
            position: fixed;
            width: 186px;
            top: 117px;
            right: 20px;
            bottom: 86px;
            overflow: auto;
            font-size: 14px;
            background-color: #fff;
            line-height: 20px;
        }

        #outline ul {
            margin-left: 16px;
            list-style: none;
        }

        #outline > ul {
            margin-left: 0;
        }

        #outline li > span {
            cursor: pointer;
            border-left: 1px solid transparent;
            display: block;
            padding-left: 8px;
        }

        #outline li > span.vditor-outline__item--current {
            border-left: 1px solid #4285f4;
            color: #4285f4;
            background-color: #f6f8fa;
        }

        #outline li > span:hover {
            color: #4285f4;
            background-color: #f6f8fa;
        }

        @media (max-width: 768px) {
            #outline {
                display: none !important;
            }
        }
    </style>
</head>
<body>
<div class="header fn-clear">
    <a class="header-logo" href="https://b3log.org/vditor">
        <img src="https://cdn.jsdelivr.net/gh/vanessa219/b3log-index@d6b3ad3964429fe682f3de3ff4a48b59ea3145d9/src/images/brand/vditor-128.png">
        <h1>Vditor</h1>
    </a>
    <div class="fn-clear">
        <a class="header-a" href="https://ld246.com/tag/vditor" target="_blank">社区</a>
        <a class="header-a header-red" target="_blank" href="https://ld246.com/article/1549638745630">API</a>
        <a class="header-a header-green current" href="index.html">Demo</a>
        <a class="header-a header-yellow" href="https://ld246.com/sponsor" target="_blank">成为赞助者</a>
    </div>
</div>
<div class="fn-50"></div>
<div class="wrapper">
    相关 <a href="https://ld246.com/article/1549638745630#static-methods" target="_blank">API</a>：
    Vditor.preview()，Vditor.outlineRender()
    <div class="fn-50"></div>
    <div id="preview"></div>
    <div class="fn-100"></div>
    <div id="vditorDemoCode" class="vditor-reset">
        <pre><code class="javascript">  const initOutline = () => {
    const headingElements = []
    Array.from(document.getElementById('preview').children).forEach((item) => {
      if (item.tagName.length === 2 && item.tagName !== 'HR' && item.tagName.indexOf('H') === 0) {
        headingElements.push(item)
      }
    })

    let toc = []
    window.addEventListener('scroll', () => {
      const scrollTop = window.scrollY
      toc = []
      headingElements.forEach((item) => {
        toc.push({
          id: item.id,
          offsetTop: item.offsetTop,
        })
      })

      const currentElement = document.querySelector('.vditor-outline__item--current')
      for (let i = 0, iMax = toc.length; i < iMax; i++) {
        if (scrollTop < toc[i].offsetTop - 30) {
          if (currentElement) {
            currentElement.classList.remove('vditor-outline__item--current')
          }
          let index = i > 0 ? i - 1 : 0
          document.querySelector('span[data-target-id="' + toc[index].id + '"]').classList.add('vditor-outline__item--current')
          break
        }
      }
    })
  }
  fetch('markdown/zh_CN.md').
  then(response => response.text()).
  then(markdown => {
    Vditor.preview(document.getElementById('preview'),
      markdown, {
        speech: {
          enable: true,
        },
        anchor: 1,
        after () {
          if (window.innerWidth <= 768) {
            return
          }
          const outlineElement = document.getElementById('outline')
          Vditor.outlineRender(document.getElementById('preview'), outlineElement)
          if (outlineElement.innerText.trim() !== '') {
            outlineElement.style.display = 'block'
            initOutline()
          }
        },
      })
  })
</code></pre>
    </div>
    <div class="fn-100"></div>
    <div class="fn-clear">
        <h2 class="fn-left">参与讨论</h2>
        <span class="fn-right" style="line-height: 36px" id="commentCnt"></span>
    </div>
    <div class="fn-50"></div>
    <div id="vditorComments"></div>
    <div class="fn-100"></div>
</div>
<div id="outline"></div>
<!-- end main -->

<div class="footer">
    <div class="wrapper fn-clear">
        <a href="https://b3log.org">首页</a> &nbsp; &nbsp;
        <a href="https://ld246.com" target="_blank">社区</a> &nbsp; &nbsp;
        <a href="https://b3log.org/brand-marking.html">品牌标识</a> &nbsp; &nbsp;
        <a href="https://ld246.com/sponsor" target="_blank">成为赞助者</a>
        <div class="fn-right">
            <a href="https://beian.miit.gov.cn/" target="_blank">滇ICP备14007358号-1</a> &nbsp; &nbsp;
            © 2010-2021 云南链滴科技有限公司
        </div>
    </div>
</div>
<script>
  const initRender = () => {
    fetch('markdown/zh_CN.md').
      then(response => response.text()).
      then(markdown => {
        Vditor.preview(document.getElementById('preview'),
          markdown, {
            speech: {
              enable: true,
            },
            anchor: 1,
            after () {
              if (window.innerWidth <= 768) {
                return
              }
              const outlineElement = document.getElementById('outline')
              Vditor.outlineRender(document.getElementById('preview'), outlineElement)
              if (outlineElement.innerText.trim() !== '') {
                outlineElement.style.display = 'block'
                initOutline()
              }
            },
          })
      })
  }

  const initOutline = () => {
    const headingElements = []
    Array.from(document.getElementById('preview').children).forEach((item) => {
      if (item.tagName.length === 2 && item.tagName !== 'HR' && item.tagName.indexOf('H') === 0) {
        headingElements.push(item)
      }
    })

    let toc = []
    window.addEventListener('scroll', () => {
      const scrollTop = window.scrollY
      toc = []
      headingElements.forEach((item) => {
        toc.push({
          id: item.id,
          offsetTop: item.offsetTop,
        })
      })

      const currentElement = document.querySelector('.vditor-outline__item--current')
      for (let i = 0, iMax = toc.length; i < iMax; i++) {
        if (scrollTop < toc[i].offsetTop - 30) {
          if (currentElement) {
            currentElement.classList.remove('vditor-outline__item--current')
          }
          let index = i > 0 ? i - 1 : 0
          document.querySelector('span[data-target-id="' + toc[index].id + '"]').classList.add('vditor-outline__item--current')
          break
        }
      }
    })
  }
  vditorScript = initRender
</script>
</body>
</html>
